package com.holly.unit.security.starter;

import com.holly.unit.cache.redis.util.CreateRedisTemplateUtil;
import com.holly.unit.security.api.DragCaptchaApi;
import com.holly.unit.security.api.ImageCaptchaApi;
import com.holly.unit.security.captcha.DragCaptchaService;
import com.holly.unit.security.captcha.ImageCaptchaService;
import com.holly.unit.security.captcha.cache.CaptchaRedisCache;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;

/**
 * 图形验证码自动配置
 *
 * @author holly
 * @date 2020/12/1 21:44
 */
@Configuration
public class CaptchaAutoConfiguration {

//    /**
//     * 图形验证码
//     *
//     * @author holly
//     * @date 2021/1/15 11:25
//     */
//    @Bean
//    @ConditionalOnMissingBean(ImageCaptchaApi.class)
//    public ImageCaptchaApi captchaApi() {
//        // 验证码过期时间 120秒
//        TimedCache<String, String> timedCache = CacheUtil.newTimedCache(1000 * 120);
//        CaptchaMemoryCache captchaMemoryCache = new CaptchaMemoryCache(timedCache);
//        return new ImageCaptchaService(captchaMemoryCache);
//    }
//
//    /**
//     * 拖拽验证码工具
//     *
//     * @author holly
//     * @date 2021/7/5 11:57
//     */
//    @Bean
//    @ConditionalOnMissingBean(DragCaptchaApi.class)
//    public DragCaptchaApi dragCaptchaService() {
//        // 验证码过期时间 120秒
//        TimedCache<String, String> timedCache = CacheUtil.newTimedCache(1000 * 120);
//        CaptchaMemoryCache captchaMemoryCache = new CaptchaMemoryCache(timedCache);
//        return new DragCaptchaService(captchaMemoryCache);
//    }
    /**
     * 图形验证码
     *
     * @author holly
     * @date 2021/1/15 11:25
     */
    @Bean
    @ConditionalOnMissingBean(ImageCaptchaApi.class)
    public ImageCaptchaApi captchaApi(RedisConnectionFactory redisConnectionFactory) {
        RedisTemplate<String, String> redisCache = CreateRedisTemplateUtil.createString(redisConnectionFactory);
        CaptchaRedisCache captchaRedisCache = new CaptchaRedisCache(redisCache);
        return new ImageCaptchaService(captchaRedisCache);
    }

    /**
     * 拖拽验证码工具
     *
     * @author holly
     * @date 2021/7/5 11:57
     */
    @Bean
    @ConditionalOnMissingBean(DragCaptchaApi.class)
    public DragCaptchaApi dragCaptchaService(RedisConnectionFactory redisConnectionFactory) {
        RedisTemplate<String, String> redisCache = CreateRedisTemplateUtil.createString(redisConnectionFactory);
        CaptchaRedisCache captchaRedisCache = new CaptchaRedisCache(redisCache);
        return new DragCaptchaService(captchaRedisCache);
    }
}
